package org.jeecg.modules.business.service.impl;

import cn.hutool.core.date.DateTime;
import org.apache.commons.lang3.ObjectUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.business.entity.BusMemberBalanceLog;
import org.jeecg.modules.business.entity.BusMemberCard;
import org.jeecg.modules.business.mapper.BusMemberBalanceLogMapper;
import org.jeecg.modules.business.mapper.BusMemberCardMapper;
import org.jeecg.modules.business.service.IBusMemberBalanceLogService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Random;
import java.util.UUID;

/**
 * @Description: bus_member_balance_log
 * @Author: jeecg-boot
 * @Date:   2023-03-13
 * @Version: V1.0
 */
@Service
public class BusMemberBalanceLogServiceImpl extends ServiceImpl<BusMemberBalanceLogMapper, BusMemberBalanceLog> implements IBusMemberBalanceLogService {
    @Resource
    private BusMemberCardMapper busMemberCardMapper;
    /**
     * 生成16位数字+prefix
     * @param prefix
     * @return
     */
    public static String randomNumber(String prefix) {
        int first = new Random(10).nextInt(8) + 1;
        int hashCode = UUID.randomUUID().toString().hashCode();
        if (hashCode < 0) {
            hashCode = -hashCode;
        }
        return prefix + first + String.format("%015d", hashCode);
    }
    /**
     * 充值/扣费余额
     *
     * @param dto
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Result saveBalance(BusMemberBalanceLog dto) {
        if (ObjectUtils.isEmpty(dto.getMemberId())) return Result.error("会员id不能为空");
        if (dto.getMoney().compareTo(BigDecimal.ZERO) < 0) return Result.error("金额不能小于0!");
        BusMemberCard member = busMemberCardMapper.selectById(dto.getMemberId());
        if (ObjectUtils.isEmpty(member)) {
            return Result.error("会员不存在");
        }
        String prefix="";
        BigDecimal balance = new BigDecimal(0);
        switch (dto.getType()) {
            case 1:
                balance = member.getBalance().add(dto.getMoney()).add(dto.getGiveMoney());
                prefix="CZ";
                break;
            case 2:
                balance = member.getBalance().subtract(dto.getMoney());
                if (dto.getWithhold() != null){
                    balance = balance.subtract(dto.getWithhold());
                }
                prefix="KF";
                break;
        }
        if (balance.compareTo(BigDecimal.ZERO) < 0) balance = new BigDecimal(0);
        member.setBalance(balance);
        Integer res = busMemberCardMapper.updateById(member);
        if (res > 0) {
            dto.setBalance(balance);
            dto.setCode(randomNumber(prefix));
            dto.setStatus(1);
            dto.setCreateTime(DateTime.now());
            this.save(dto);
        }
        return Result.ok("创建成功");
    }
}
